package com.leyou.item.services.impl;

import com.leyou.common.enums.ExceptionEnums;
import com.leyou.common.exception.LyException;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.pojo.Category;
import com.leyou.item.services.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 分类管理业务
 * @Date 2018/12/11  21:17
 * @Author JIE
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
   private CategoryMapper categoryMapper;

    /**
     * 根据父id查询分类
     * @param pid
     * @return
     */
    @Override
    public List<Category> queryCategoryByPid(Long pid) {
        //查询条件，mapper会把对象中的非空属性作为查询条件
        Category category = new Category();
        category.setParentId(pid);
        List<Category> categoryList = categoryMapper.select(category);
        //判断查询结果
        if (CollectionUtils.isEmpty(categoryList)){
           throw new LyException(ExceptionEnums.CATEGORY_NOT_FOND);
        }
        return categoryList;
    }

    @Override
    public List<Category> queryByBrandId(Long bid) {
        return categoryMapper.queryByBrandId(bid);
    }

    @Override
    public List<Category> queryByIds(List<Long> ids) {
        List<Category> categoryList = categoryMapper.selectByIdList(ids);
        if (CollectionUtils.isEmpty(categoryList)){
            throw new LyException(ExceptionEnums.CATEGORY_NOT_FOND);
        }
        return categoryList;
    }
}
